﻿function m_DaysInMonth(pYear, pMonth) {
    var nDays = 30;
    if (pMonth == 2) {    //2月单独处理
        if (m_IsLeap(pYear)) nDays = 29;
        else nDays = 28;
    }
    else if (mInList(pMonth, "1,3,5,7,8,10,12")) {
        nDays = 31;
    }
    return nDays;
}

//判断某年是否是闰年
function m_IsLeap(pYear) {
    if (pYear % 400 == 0) {
        return true;
    }
    else if ((pYear % 100 != 0) && (pYear % 4 == 0)) {
        return true;
    }
    return false;
}

function mInList(s1, slist) {
    var ss1 = "," + s1 + ",";
    var ss2 = "," + slist + ",";
    ss1 = ss1.toLowerCase();
    ss2 = ss2.toLowerCase();
    return ss2.indexOf(ss1) != -1;
}

function m_MonthSelect(id) {
    var yearid = id.substring(0, id.indexOf("ddl3Month")) + "ddl3Year";
    var dayid = id.substring(0, id.indexOf("ddl3Month")) + "ddl3Day";
    var theselectedyear = document.getElementById(yearid);
    var i = theselectedyear.selectedIndex;
    var yearselectedvalue = theselectedyear.options[i].value;

    var theselectedmonth = document.getElementById(id);
    var y = theselectedmonth.selectedIndex;
    var monthselectedvalue = theselectedmonth.options[y].value;

    var temp = document.getElementById(dayid);
    temp.length = m_DaysInMonth(yearselectedvalue, monthselectedvalue);
    for (i = 0; i < temp.length; i++) {
        temp.options[i] = new Option(i + 1, i + 1);
    }
    temp.selectedIndex = new Date().getDate() - 1;
}

function m_YearSelect(id) {
    var monthid = id.substring(0, id.indexOf("ddl3Year")) + "ddl3Month";
    var dayid = id.substring(0, id.indexOf("ddl3Year")) + "ddl3Day";
    var theselectedmonth = document.getElementById(monthid);
    var i = theselectedmonth.selectedIndex;
    var monthselectedvalue = theselectedmonth.options[i].value;

    var theselectedyear = document.getElementById(id);
    var y = theselectedyear.selectedIndex;
    var yearselectedvalue = theselectedyear.options[y].value;

    if (monthselectedvalue == "2") {
        var temp = document.getElementById(dayid);
        temp.length = m_DaysInMonth(yearselectedvalue, monthselectedvalue);
        for (i = 0; i < temp.length; i++) {
            temp.options[i] = new Option(i + 1, i + 1);
        }
        temp.selectedIndex = new Date().getDate() - 1;
    }
}